<?php
//$HeadURL: http://instaforms.googlecode.com/svn/trunk/instaforms_endpoint.php $
//$Revision: 20 $
//$Date: 2012-08-27 18:55:18 +0000 (Mon, 27 Aug 2012) $
//$Author: dev@arbrebinaire.com $
/**
 *
 * This script receives AJAX calls from main.js
 *
 * PHP version 5
 *
 * @package    com.arbrebinaire.instaforms
 * @author     Francois Hetu <dev@arbrebinaire.com>
 * @copyright  2012 Francois Hetu
 * @version    SVN: $Id: instaforms_endpoint.php 20 2012-08-27 18:55:18Z dev@arbrebinaire.com $
 */

include_once '/lib/bootstrap.php';


$core = new InstaFormsCore();

$request = $imp->getPostRequest(null);

if($imp->isAjaxPostRequest($request) && $imp->userCanModifyForm(null)){
    //Valid request
    if($core->isSaveRequest($request)){
        
        $form = $core->getFormData($request);
        
        $response = $core->prepareSaveResponse($imp->saveFormData($form));
        
        $imp->sendData($response);
        
    }elseif($core->isLoadRequest($request)){
        
        $requestedFormId = $core->getRequestedFormId($request);
        
        $data = $core->prepareFormData($imp->getFormData($requestedFormId));
        
        $imp->sendData($data);
        
    }else{
       //Not any of the request type recognized by InstaForms
       $imp->sendEmptyResponse(null);
        
    }
}else{
    //Invalid request
    $imp->sendEmptyResponse(null);
}